<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Order;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Table;
use Faker\Factory;
use Illuminate\Support\Facades\Storage;

class OrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Order(), function (Grid $grid) {
            $grid->model()->latest('id');
            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function ($id) {
                return User::find($id)->name ?? '';
            });
            $grid->column('sell_type')->using(\App\Models\Order::sellTypeMap());
            $grid->column('sell_json')
                ->display(function ($value) {
                    $sellJson = json_decode($value, true);
                    return $sellJson['name'];
                })
                ->expand(function ($model) {
                    $row = $model->row;

                    $sellJson = json_decode($row->sell_json, true) ?? [];
                    $sellMultiJson = json_decode($row->sell_multi_json, true) ?? [];

                    $data = [];
                    if ($row->sell_type == \App\Models\Order::SELL_FOOD) {
                        foreach ($sellMultiJson as $item) {
                            $img = Storage::url($item['img'] ?? '');
                            $data[] = [
                                $item['id'] ?? '',
                                $item['name'] ?? '',
                                "<img src={$img} width='50'>",
                                $item['price'] ?? '',
                                $item['num'] ?? '',
                                $item['remark'] ?? '',
                            ];
                        }
                        return Table::make(['ID', '名称', '图片', '价格', '数量', '备注'], $data);
                    } else {
                        $img = Storage::url($sellJson['img'] ?? '');
                        $data[] = [
                            $sellJson['id'] ?? '',
                            $sellJson['name'] ?? '',
                            "<img src={$img} width='50'>",
                            $sellJson['price'] ?? '',
                        ];
                        if ($row->sell_type == \App\Models\Order::SELL_COMBO && $sellMultiJson) {
                            foreach ($sellMultiJson as $item) {
                                $img = Storage::url($item['img'] ?? '');
                                $data[] = [
                                    $item['id'] ?? '',
                                    $item['name'] ?? '',
                                    "<img src={$img} width='50'>",
                                    $item['price'] ?? '',
                                ];
                            }
                        }
                    }
                    return Table::make(['ID', '名称', '图片', '价格'], $data);
                });
            $grid->column('order_sn');
            $grid->column('transaction_id');
            $grid->column('pay_type')->using(\App\Models\Order::payTypeMap());
            $grid->column('pay_price');
            $grid->column('phone');
            $grid->column('pre_num');
            $grid->column('use_num');
            $grid->column('pre_date');
            $grid->column('pre_time');
            $grid->column('pay_time');
            $grid->column('state')->using(\App\Models\Order::stateMap());
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user_id')->select(User::pluck('name', 'id'));
                $filter->equal('sell_type')->select(\App\Models\Order::sellTypeMap());
                $filter->where('sell_json', function ($query) {
                    $query->where('sell_json->name', 'like', "%{$this->input}%");
                });
                $filter->like('order_sn');
                $filter->like('transaction_id');
                $filter->equal('pay_type')->select(\App\Models\Order::payTypeMap());
                $filter->between('pay_price');
                $filter->like('phone');
                $filter->between('pre_date')->datetime();
                $filter->between('pay_time')->datetime();
                $filter->equal('state')->select(\App\Models\Order::stateMap());
                $filter->between('created_at')->datetime();
            });
            $grid->quickSearch(['id', 'order_sn', 'transaction_id', 'sell_multi_json']);
            // 禁用
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            // 导出
            $grid->export()->rows(function ($rows) {
                $userNames = User::pluck('name', 'id');
                $sellType = \App\Models\Order::sellTypeMap();
                $payType = \App\Models\Order::payTypeMap();
                $state = \App\Models\Order::stateMap();
                foreach ($rows as $index => &$row) {
                    $row['user_id'] = $userNames[$row['user_id']] ?? '';
                    $sellJson = json_decode($row['sell_json'], true) ?? [];
                    $row['sell_json'] = $sellJson['name'] ?? '';
                    $row['sell_type'] = $sellType[$row['sell_type']];
                    $row['pay_type'] = $payType[$row['pay_type']];
                    $row['state'] = $state[$row['state']];
                }

                return $rows;
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('camp_id');
            $show->field('sell_type');
            $show->field('sell_id');
            $show->field('order_sn');
            $show->field('transaction_id');
            $show->field('pay_type');
            $show->field('pay_price');
            $show->field('phone');
            $show->field('pre_num');
            $show->field('use_num');
            $show->field('pre_date');
            $show->field('pre_time');
            $show->field('sell_json');
            $show->field('sell_multi_json');
            $show->field('pay_expire_time');
            $show->field('pay_time');
            $show->field('user_delete_time');
            $show->field('state');
            $show->field('created_at');
            $show->field('updated_at');
            // 禁用
            $show->disableEditButton();
            $show->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(), function (Form $form) {
            $form->display('id');
            $form->display('user_id');
            $form->display('camp_id');
            $form->display('sell_type');
            $form->display('sell_id');
            $form->display('order_sn');
            $form->display('transaction_id');
            $form->display('pay_type');
            $form->display('pay_price');
            $form->display('phone');
            $form->display('pre_num');
            $form->display('use_num');
            $form->display('pre_date');
            $form->display('pre_time');
            $form->display('sell_json');
            $form->display('sell_multi_json');
            $form->display('pay_expire_time');
            $form->display('pay_time');
            $form->display('user_delete_time');
            $form->display('state');
        });
    }
}
